package com.sinosoft.perfect.system._roleSystem.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.common.system.BaseAction;
import com.sinosoft.perfect.system._.po.TRoleSystem;
import com.sinosoft.perfect.system._roleSystem.dataFormat.IRoleSystemGridDataFormat;
import com.sinosoft.perfect.system._roleSystem.service.IRoleSystemService;

@SuppressWarnings("serial")
public class RoleSystemAction extends BaseAction {

	private IRoleSystemService roleSystemService;

	private IRoleSystemGridDataFormat roleSystemGridDataFormat;

	private TRoleSystem troleSystem;

	private String roleSystemId;

	private String unitId;

	private String userId;

	/**
	 * 
	 */
	public String roleSystemGrid() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TRoleSystem> roleSystems = roleSystemService.selectByTRoleSystem(troleSystem);
			resultXml = roleSystemGridDataFormat.dataFormatRoleSystemGrid(roleSystems);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String roleSystemGridForResource() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TRoleSystem> roleSystems = roleSystemService.selectByTRoleSystem(troleSystem);
			resultXml = roleSystemGridDataFormat.dataFormatRoleSystemGridForResource(roleSystems);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String roleSystemGridForBindUnit() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TRoleSystem> roleSystems = roleSystemService.selectByTRoleSystem(troleSystem);
			List<String> roleSystemIds = roleSystemService.selectTRoleSystemFromUnit(unitId);
			resultXml = roleSystemGridDataFormat.dataFormatRoleSystemGridForBindUser(roleSystems, roleSystemIds);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String roleSystemGridForBindUser() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TRoleSystem> roleSystems = roleSystemService.selectByTRoleSystem(troleSystem);
			List<String> roleSystemIds = roleSystemService.selectTRoleSystemFromUser(userId);
			resultXml = roleSystemGridDataFormat.dataFormatRoleSystemGridForBindUser(roleSystems, roleSystemIds);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}
	
	/**
	 * 为组织机构用户配置(fys 11-07-11)，主要是拷贝上面的方法，将grid的列变成一列
	 */
	public String roleSystemGridForBindAUnitUsers() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TRoleSystem> roleSystems = roleSystemService.selectByTRoleSystem(troleSystem);
			List<String> roleSystemIds = roleSystemService.selectTRoleSystemFromUser(userId);
			resultXml = roleSystemGridDataFormat.roleSystemGridForBindAUnitUsers(roleSystems, roleSystemIds);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}
	
	/**
	 * 为组织机构用户配置(fys 11-07-11)，主要是拷贝roleSystemGridForBindUnit方法，将grid的列变成一列
	 */
	public String roleSystemGridForBindAUnit() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TRoleSystem> roleSystems = roleSystemService.selectByTRoleSystem(troleSystem);
			List<String> roleSystemIds = roleSystemService.selectTRoleSystemFromUnit(unitId);
			resultXml = roleSystemGridDataFormat.roleSystemGridForBindAUnit(roleSystems, roleSystemIds);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	/**
	 * get/set
	 */

	public void setRoleSystemService(IRoleSystemService roleSystemService) {
		this.roleSystemService = roleSystemService;
	}

	public void setRoleSystemGridDataFormat(IRoleSystemGridDataFormat roleSystemGridDataFormat) {
		this.roleSystemGridDataFormat = roleSystemGridDataFormat;
	}

	public TRoleSystem getTroleSystem() {
		return troleSystem;
	}

	public void setTroleSystem(TRoleSystem troleSystem) {
		this.troleSystem = troleSystem;
	}

	public String getRoleSystemId() {
		return roleSystemId;
	}

	public void setRoleSystemId(String roleSystemId) {
		this.roleSystemId = roleSystemId;
	}

	public String getUnitId() {
		return unitId;
	}

	public void setUnitId(String unitId) {
		this.unitId = unitId;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

}
